var express = require('express');
var router = express.Router();

var https = require('https');

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


//天气情况
router.get('/weather',function(req,res){
    var latitude = req.query.latitude;
    var longitude = req.query.longitude;
// 'https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude
    https.get('https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude,function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})
//搜索框下面的热搜
router.get('/hot_search',function(req,res){
    var latitude = req.query.latitude;
    var longitude = req.query.longitude;
// 'https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude
    https.get('https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?latitude='+latitude+'&longitude='+longitude,function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})
//外卖轮播
router.get('/slideshow',function(req,res){
    // var latitude = req.query.latitude;
    // var longitude = req.query.longitude;
// 'https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude
    https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?latitude=22.58329920000001&longitude=113.9569964&templates[]=main_template',function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})

//商家推荐
router.get('/recommend',function(req,res){
    // var latitude = req.query.latitude;
    // var longitude = req.query.longitude;
    var page = req.query.page;
// 'https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude
    https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=22.58329920000001&longitude=113.9569964&offset='+ page +'&limit=20&extras[]=activities&terminal=h5',function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})
//详情页面
router.get('/detail',function(req,res){
    var latitude = req.query.latitude;
    var longitude = req.query.longitude;
    var id = req.query.id;

    https.get('https://mainsite-restapi.ele.me/shopping/restaurant/'+ id +'?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude='+ latitude +'&longitude='+longitude,function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})

//详情列表
router.get('/information',function(req,res){
    var latitude = req.query.latitude;
    var longitude = req.query.longitude;
    var id = req.query.id;

    https.get('https://mainsite-restapi.ele.me/shopping/v2/menu?restaurant_id='+id,function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})


//gift
router.get('/gift',function(req,res){
    // var latitude = req.query.latitude;
    // var longitude = req.query.longitude;
// 'https://mainsite-restapi.ele.me/bgs/weather/current?latitude='+latitude+'&longitude='+longitude
    https.get('https://mainsite-restapi.ele.me/member/gifts/suggest',function(response){

        var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            // console.log(data);
            res.send(data)
        })
    })
})

//list页面请求的数据
router.get('/list',function(req,res){
    https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=22.54286&longitude=114.059563&keyword=&offset=0&limit=20&extras[]=activities&restaurant_category_ids[]=209&restaurant_category_ids[]=211&restaurant_category_ids[]=212&restaurant_category_ids[]=213&restaurant_category_ids[]=214&restaurant_category_ids[]=215&restaurant_category_ids[]=216&restaurant_category_ids[]=217&restaurant_category_ids[]=218&restaurant_category_ids[]=219&restaurant_category_ids[]=221&restaurant_category_ids[]=222&restaurant_category_ids[]=223&restaurant_category_ids[]=224&restaurant_category_ids[]=225&restaurant_category_ids[]=226&restaurant_category_ids[]=227&restaurant_category_ids[]=228&restaurant_category_ids[]=229&restaurant_category_ids[]=230&restaurant_category_ids[]=231&restaurant_category_ids[]=232&restaurant_category_ids[]=234&restaurant_category_ids[]=235&restaurant_category_ids[]=236&restaurant_category_ids[]=237&restaurant_category_ids[]=238&restaurant_category_ids[]=263&restaurant_category_ids[]=264&restaurant_category_ids[]=265&restaurant_category_ids[]=266&restaurant_category_ids[]=267&restaurant_category_ids[]=268&restaurant_category_ids[]=269',function(response){
         var data='';
        response.on('data',function(chunk){
            data+=chunk;
        })

        response.on('end',function(){
            res.send(data)
        })
    })
})


module.exports = router;
